package cn.imvc.app.models;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Comments;
import cn.imvc.app.repositories.CommentsRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class CommentsModelImpl implements CommentsModel {

	@Autowired
	CommentsRepository commentsRepository;
	@Autowired
	GoodsModel goodsModel;
	
	public Page<Comments> find(ViewRequest viewRequest) {
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
		return commentsRepository.findAll("distinct c","Comments c left join c.member m left join c.goods g",viewRequest);
	}

	public Comments get(int id) {
		return commentsRepository.getOne(id);
	}

	public Comments save(Comments comments) {
		// TODO Auto-generated method stub
		return null;
	}

	public Comments update(Comments comments) {
		Comments commentsTmp = get(comments.getId());
		commentsTmp.setState(comments.getState());
		commentsTmp.setReply(comments.getReply());
		commentsTmp.setRemark(comments.getRemark());
		if(commentsTmp.getState() == (byte)1){
			goodsModel.updateComments(commentsTmp.getGoods().getId());
		}
		return commentsRepository.saveAndFlush(commentsTmp);
	}

	public void delete(int id) {
		// TODO Auto-generated method stub

	}

	public void deleteBatch(int[] ids) {
		// TODO Auto-generated method stub

	}

}
